package com.huoyun.userAdmin.action;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//import javax.enterprise.inject.New;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.StrutsStatics;
import org.springframework.security.authentication.encoding.PasswordEncoder;

import com.huoyun.base.action.BaseAction;
import com.huoyun.userAdmin.bean.UserDetail;
import com.huoyun.userAdmin.service.UserDetailService;
import com.huoyun.user.bean.User;
import com.huoyun.user.service.UserService;
import com.huoyun.util.DateUtil;
import com.huoyun.util.FileUtil;
import com.huoyun.util.StringUtil;
import com.opensymphony.xwork2.ActionContext;

public class AccountSecureAction extends BaseAction {

	private static final Log log = LogFactory.getLog(AccountSecureAction.class);
	
	private UserService userService;
	
	private UserDetailService userDetailService;
	
	private PasswordEncoder passwordEncoder;
	
	/**
	 * 密码
	 */
	private String securePassword;
	
	/**
	 * 旧密码
	 */
	private String oldSecurePassword;
	
	/**
	 * 新密码
	 */
	private String newSecurePassword;
	
	/**
	 * 确认密码
	 */
	private String verifySecurePassword;
	
	/**
	 * 用户对象
	 */
	private UserDetail userDetail;

	public String preCreateSecurePassword()
	{
		String password = getLoginUser().getSecurePassword();
		if (password == null || password.equals(""))
			return "create";
		else
			return "update";
	}
	
	public String createSecurePassword()
	{
		String password = getLoginUser().getPassword();
		newSecurePassword = passwordEncoder.encodePassword(newSecurePassword, getLoginUser().getSalt());
		
		if (password.equals(newSecurePassword)) {
			this.setResultMsg("安全密码和登录密码不能相同");
			return INPUT;
		}
		userService.changeSecurePassword(getLoginUser().getId(), newSecurePassword);
		getLoginUser().setSecurePassword(newSecurePassword);
		
		this.setResultMsg("密码修改成功");
		
		return SUCCESS;
	}
	
	public String checkPassword() throws IOException
	{
		this.setResultMsg("false");
		String password = getLoginUser().getPassword();
		String securePassword = passwordEncoder.encodePassword(newSecurePassword, getLoginUser().getSalt());

		if (!password.equals(securePassword)) {
			this.setResultMsg("true");
		}		
		
		HttpServletResponse response = (HttpServletResponse)ActionContext.getContext().get(ServletActionContext.HTTP_RESPONSE);
		
		try
		{
			response.getWriter().print(this.getResultMsg());
		}catch (IOException e) {
			log.error("校验密码输出流发生错误！原因：" + e.fillInStackTrace());
			
			throw e;
		}
		return null;	
	}
	
	public String changeSecurePassword()
	{
		if (newSecurePassword.equals(oldSecurePassword)) {
			this.setResultMsg("新密码不能和当前密码相同");
			return INPUT;
		}
			
		oldSecurePassword = passwordEncoder.encodePassword(oldSecurePassword, getLoginUser().getSalt());
		String str = getLoginUser().getSecurePassword();
		if(getLoginUser().getSecurePassword().equals(oldSecurePassword))
		{
			newSecurePassword = passwordEncoder.encodePassword(newSecurePassword, getLoginUser().getSalt());
			userService.changeSecurePassword(getLoginUser().getId(), newSecurePassword);
			
			getLoginUser().setSecurePassword(newSecurePassword);
			
			this.setResultMsg("安全密码修改成功");
		}
		else
		{
			this.setResultMsg("当前安全密码输入错误");
			return INPUT;
		}
		
			
		return SUCCESS;
	}
	
	public String verifySecurePassword() 
	{
		if (securePassword != null && passwordEncoder.encodePassword(securePassword, getLoginUser().getSalt()).equals(getLoginUser().getSecurePassword())) {
	        getSession().setAttribute("securePasswordVerified", "true");
			try {
				Map<String, String[]> parameterMap = (Map<String, String[]>)getSession().getAttribute("parameterMapBeforeSecure");
				String paramStr = "";
				if (parameterMap != null) {
					int count = 0;
					for (String param : parameterMap.keySet()) {
						if (count == 0) {
							paramStr += ("?" + param + "=" + parameterMap.get(param)[0]);
						} else {
							paramStr += ("&" + param + "=" + parameterMap.get(param)[0]);
						}
					}
				}
				getResponse().sendRedirect((String)getSession().getAttribute("urlBeforeSecure") + paramStr);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return NONE;
		} else {
			setResultMsg("安全密码验证失败，请重试！");
			return INPUT;
		}
	}
	
	public String preEditAvatar()
	{
		return SUCCESS;
	}
	
	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public PasswordEncoder getPasswordEncoder() {
		return passwordEncoder;
	}

	public void setPasswordEncoder(PasswordEncoder passwordEncoder) {
		this.passwordEncoder = passwordEncoder;
	}

	public String getNewSecurePassword() {
		return newSecurePassword;
	}

	public void setNewSecurePassword(String newSecurePassword) {
		this.newSecurePassword = newSecurePassword;
	}

	public String getVerifySecurePassword() {
		return verifySecurePassword;
	}

	public void setVerifySecurePassword(String verifySecurePassword) {
		this.verifySecurePassword = verifySecurePassword;
	}

	public UserDetail getUserDetail() {
		return userDetail;
	}

	public void setUserDetail(UserDetail userDetail) {
		this.userDetail = userDetail;
	}

	public UserDetailService getUserDetailService() {
		return userDetailService;
	}

	public void setUserDetailService(UserDetailService userDetailService) {
		this.userDetailService = userDetailService;
	}

	public String getOldSecurePassword() {
		return oldSecurePassword;
	}

	public void setOldSecurePassword(String oldSecurePassword) {
		this.oldSecurePassword = oldSecurePassword;
	}

	public String getSecurePassword() {
		return securePassword;
	}

	public void setSecurePassword(String securePassword) {
		this.securePassword = securePassword;
	}

}
